package com.tty.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tty.entity.Url;
import com.tty.entity.User;
import com.tty.mapper.UrlMapper;
import com.tty.mapper.UserMapper;
import com.tty.service.UrlService;
import com.tty.service.UserService;
import com.tty.utils.UrlUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName UserController
 * @Description TODO
 * @Author Tommy Yeung
 * @Date 2022/11/21 上午 10:39
 **/

@Controller
public class UserController {


    @Autowired
    UserMapper userMapper;

    @Autowired
    UserService userService;

    @Autowired
    UrlMapper urlMapper;

    @Autowired
    UrlService urlService;

    @GetMapping("/login")
    public String log(){
        return "login";
    }

    @PostMapping( "/login")
    public String login(HttpServletRequest request,User user,Model model){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User us = userMapper.selectOne(queryWrapper);

        if (us != null){
            //用户存在
            if (user.getPassword().equals(us.getPassword())){
                //密码正确
                HttpSession session = request.getSession();
                session.setAttribute("user", us);
                session.setAttribute("separate", us.getSeparate());
                Object user1 = session.getAttribute("user");
                System.out.println(user1);
                if(("A").equals(us.getRole())){
                    //为管理员
//                    request.getSession().setAttribute("user",us.getId());
                    return "redirect:/admin1";
                }else {
                    //普通用户
//                    request.getSession().setAttribute("user",us.getId());
                    List<Url> myUrl = urlService.findMyUrl(us.getId());
                    model.addAttribute("myUrl",myUrl);
                    return "index";
                }
            }
        }
        return "login";
    }

    @GetMapping("/reg")
    public String reg(){
        return "register";
    }

    @PostMapping("/reg")
    public String register(HttpServletRequest request,User user){
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername,user.getUsername());
        User us = userMapper.selectOne(queryWrapper);


        if (us == null){
            //用户名不存在可以注册
            user.setSeparate(UrlUtils.generateRandomCode(5));
            String md5Pw = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
            user.setPassword(md5Pw);
            userMapper.insert(user);
        }
        return "login";
    }

    @GetMapping("/index")
    public String index(){
        return "index";
    }

    @GetMapping("/logout")
    public String logout(HttpSession session){
        session.invalidate();
        return "login";
    }

}
